import { sendEmail } from '../controllers/utils';

//日志堆栈
var logstack = [];
var maxLogStackLength = 5;

//日志组件
//正常操作
export function i(msg) {
	logrecord(msg, 'info');
}

export function major(msg) {
	logrecord(msg, 'major')
	if (__dirname.indexOf('yangshangwei') > 0 || __dirname.indexOf('server') > 0) {
		return;
	}
	email(msg, 'MAJOR');
}

//异常操作
export function sendLogToEmail(msg) {
    let message = msg;
    if (msg instanceof Error) {
        message = msg.stack;
	}
	logrecord(message, 'error')
	var logMessage = '[' + (new Date()).toLocaleString() + '][err]' + message;
	if (__dirname.indexOf('yangshangwei') > 0 || __dirname.indexOf('server') > 0 || message.indexOf('302 Found') > 0) {
		return;
	}
	email(logMessage, 'ERROR');
}

function logrecord(msg, type) {
	var logMessage = '[' + (new Date()).toLocaleString() + '][' + type + ']' + msg;
	console.log(logMessage)
}

//邮件提交错误
function email(msg, type) {
	var subject = type + " report from " + __dirname;
	if (type == 'MAJOR') {
		subject += msg;
	}
	sendEmail('no-reply@aidoor.net', 'weigang@androifi.com;cyan@androifi.com', subject, msg);
}
